package top.shiyiri.servlet2;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @author Aunean
 * @date 2022/1/27 17:28
 */
public class ResponseIOServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        //往客户端回传字符串数据
//        resp.setCharacterEncoding("UTF-8");//设置字符编码
//        //通过响应头，设置浏览器也使用UTF-8格式的编码
//        resp.setHeader("Content-Type", "text/html; charset=UTF-8");

        //setContentType()：同时设置服务器和客户端都使用指定的字符集，还设置了响应头
        //此方法一定要在获取流对象之前设置
        resp.setContentType("text/html; charset=UTF-8");
        System.out.println(resp.getCharacterEncoding());

        PrintWriter writer = resp.getWriter();
        writer.write("十日谈");

//        ServletOutputStream outputStream = resp.getOutputStream();
//        System.out.println(outputStream);
    }
}
